﻿using System;
using System.Data;
namespace MLogix
{
    sealed internal class Saver
    {

        static private DB messageDB = new DB(Win32.location);
        static void Main(string[] args)
        {
            Win32.SetCursor(IntPtr.Zero);
            string arg = string.Empty;
            if (NotificationControl.GetStatus())
            {
                String mode = string.Empty;
                mode = NotificationControl.GetMode() ? "SILENT" : "LOUD";
                if (NotificationControl.GetQuickStartMode())
                {
                    arg = NotificationControl.GetFilterPath();
                }
                if (!string.IsNullOrEmpty(arg.Trim()))
                {
                    ProcessCE.StartProcess("Notification.exe", "\"" + NotificationControl.GetMessage().Trim() + "\"" + " " + mode + " \"" + arg + "\"");
                }
                else
                {
                    ProcessCE.StartProcess("Notification.exe", "\"" + NotificationControl.GetMessage().Trim() + "\"" + " " + mode);
                }
            }
            //StreamWriter w = new StreamWriter(Win32.location+"\\log.txt");
            //w.WriteLine(args.Length.ToString());
            if (args.Length > 0)
            {
                bool _name = false;
                bool _body = false;
                bool _addr = false;
                string name = string.Empty;
                string body = string.Empty;
                string address = string.Empty;
                foreach (string str in args)
                {
                    if (str.Equals("MLogix.SNAME"))
                    {
                        _name = true;
                        _body = false;
                        _addr = false;
                        continue;
                    }
                    if (str.Equals("MLogix.MESSAGE"))
                    {
                        _name = false;
                        _body = true;
                        _addr = false;
                        continue;
                    }
                    if (str.Equals("MLogix.ADDR"))
                    {
                        _name = false;
                        _body = false;
                        _addr = true;
                        continue;
                    }
                    if (_name && !_body && !_addr)
                    {
                        name += str + " ";
                    }
                    if (!_name && _body && !_addr)
                    {
                        body += str + " "; 
                    }
                    if (!_name && !_body && _addr)
                    {
                        address += str + " ";
                    }

                }
                NotificationControl.SetNewMessageContact(name);
              //  w.WriteLine(name + " " + address + " " + body);
                string time = DateTime.Now.ToString();
                SaveSMSToDB(CryptLib.SimpleConfuscator(name.Trim()), CryptLib.SimpleConfuscator(address.Trim()), CryptLib.SimpleConfuscator(body.Trim()), time);
            }
            //w.Close();
            //w.Dispose();
        }

        static void SaveSMSToDB(string name,string address, string body, string time)
        {
            DataTable dummy;
            messageDB.SetConnection("INSERT INTO Messages(Name,Number,Body,Sent) VALUES('" + name + "','" + address + "','" + body + "','" + time + "')", DB.sqlAction.Insert,out dummy);
            messageDB.SetConnection("INSERT INTO NewMessages(Name,Number,Body,Sent) VALUES('" + name + "','" + address + "','" + body + "','" + time + "')", DB.sqlAction.Insert, out dummy);
        }

    }
}